package com.app.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignUtils {
    /**
     * 构造签名字符串
     *
     * @param requestInfo 请求对象
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String buildSign(RequestInfo requestInfo) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String sortString = "appid=" + requestInfo.getAppid();
        sortString += "&data=" + requestInfo.getData();
        sortString += "&format=" + requestInfo.getFormat();
        sortString += "&method=" + requestInfo.getMethod();
        sortString += "&nonce=" + requestInfo.getNonce();
        sortString += "&timestamp=" + requestInfo.getTimestamp();
        sortString += "&version=" + requestInfo.getVersion();
        String forSignString = sortString + "&appsecret=" + requestInfo.getAppSecret();
        forSignString = forSignString.toLowerCase();
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(forSignString.getBytes("utf-8"));
        String sign = convertBinaryToHexValueString(bytes);
        return sign.toLowerCase();
    }

    /**
     * 转换二进制为16进制字符串
     *
     * @param bytes
     * @return
     */
    private static String convertBinaryToHexValueString(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (bytes == null || bytes.length <= 0) {
            return "";
        }
        for (byte b : bytes) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 请求对象
     */
    public static class RequestInfo {
        /**
         * 供应商appid
         */
        private String appid;
        /**
         * 供应商appSecret
         */
        private String appSecret;
        /**
         * json数据字符串
         */
        private String data;
        /**
         * 格式，json
         */
        private String format;
        /**
         * 接口标识符，比如upload.envMonitor
         */
        private String method;
        /**
         * 随机数
         */
        private String nonce;
        /**
         * 时间戳
         */
        private String timestamp;
        /**
         * 版本，比如1.0
         */
        private String version;

        public String getAppid() {
            return appid;
        }

        public void setAppid(String appid) {
            this.appid = appid;
        }

        public String getAppSecret() {
            return appSecret;
        }

        public void setAppSecret(String appSecret) {
            this.appSecret = appSecret;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public String getFormat() {
            return format;
        }

        public void setFormat(String format) {
            this.format = format;
        }

        public String getMethod() {
            return method;
        }

        public void setMethod(String method) {
            this.method = method;
        }

        public String getNonce() {
            return nonce;
        }

        public void setNonce(String nonce) {
            this.nonce = nonce;
        }

        public String getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }
    }

    public static void main(String[] args) {
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setAppid("5HGSVWBX8U");
//        requestInfo.setAppSecret();
    }
}
